import { createRouter, createWebHistory } from 'vue-router'
import { default as routes } from './routes';
import { BASE_URL, LOGIN_ROUTE_NAME } from '@/common/constants';
import { getLocalToken } from '@/common/token.js';

const router = createRouter({
	history: createWebHistory(BASE_URL),
	routes
});

//白名单
const whiteList = [LOGIN_ROUTE_NAME];
const restrictedPrefix = ['/admin'];

//全局前置导航守卫
router.beforeEach(async (to, from) => {
	if(getLocalToken()!==null && getLocalToken()!==''){
		console.log('路由守卫：非空Token');
		return true;
	}else if(restrictedPrefix.find(routerName =>to.path.startsWith(routerName))){
		console.log('路由守卫：重定向到登陆');
		return { name: 'Login', replace: true, query: {from: to.path}};
	}
	console.log('路由守卫：默认放行');
	return true;
	// if (whiteList.find(routeName => routeName === to.name)) {
	// 	return true;
	// } else if (getLocalToken()) {
	// 	return true;
	// } else {
	// 	return { name: LOGIN_ROUTE_NAME, replace: true };
	// }

})

export default router;